package com.grq.mis.service;

import com.grq.mis.dao.PermissionDao;
import com.grq.mis.dao.RoleDao;
import com.grq.mis.dao.RolePermissionDao;
import com.grq.mis.dao.UserRoleDao;
import com.grq.mis.entity.Permission;
import com.grq.mis.entity.Role;
import com.grq.mis.entity.RolePermission;
import com.grq.mis.entity.UserRole;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Transactional
@Service
public class RoleService extends BaseService<Role>{

    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private UserRoleDao userRoleDao;

    public void addRole(Role role, Long[] permissionIds) {

        // 查权限
        List<Permission> permissions = new ArrayList<>();
        if (permissionIds != null && permissionIds.length > 0) {
            permissions = permissionDao.createLambdaQuery().andIn(Permission::getId, Arrays.asList(permissionIds)).select();
        }

        // 添加角色
        roleDao.insert(role);

        // 添加关联数据
        List<RolePermission> rolePermissions = new ArrayList<>();
        for (Permission permission : permissions) {
            RolePermission rolePermission = new RolePermission(role.getId(), permission.getId());
            rolePermissions.add(rolePermission);
        }
        rolePermissionDao.insertBatch(rolePermissions);
    }

    public void updateRole(Role model, Long[] permissionIds) {
        //  验证角色
        Role role = roleDao.single(model.getId());
        if (role == null) {
            throw new BusinessException(ResultEnum.ROLE_IS_NULL);
        }

        // 修改
        role.setName(model.getName());
        role.setDescription(model.getDescription());
        roleDao.updateById(role);

        // 先删除原有关联 再添加
        rolePermissionDao.createLambdaQuery().andEq(RolePermission::getRoleId, role.getId()).delete();

        if (permissionIds != null && permissionIds.length > 0) {
            List<Permission> permissions = permissionDao.createLambdaQuery().andIn(Permission::getId, Arrays.asList(permissionIds)).select();
            List<RolePermission> rolePermissions = new ArrayList<>();
            for (Permission permission : permissions) {
                RolePermission rolePermission = new RolePermission(role.getId(), permission.getId());
                rolePermissions.add(rolePermission);
            }
            rolePermissionDao.insertBatch(rolePermissions);
        }
    }

    public void deleteRole(Long id) {
        // 验证角色信息
        Role role = roleDao.single(id);
        if (role == null) {
            throw new BusinessException(ResultEnum.ROLE_IS_NULL);
        }

        // 删除关联
        userRoleDao.createLambdaQuery().andEq(UserRole::getRoleId, role.getId()).delete();
        rolePermissionDao.createLambdaQuery().andEq(RolePermission::getRoleId, role.getId()).delete();

        // 删除实体
        roleDao.deleteById(id);
    }

    public List<Role> findByUserId(Long userId) {
        return roleDao.findByUserId(userId);
    }
}
